package com.chzc.fm.duotin.ui.other;

import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.event.LoginEvent;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.widget.XEditText;

import org.greenrobot.eventbus.EventBus;

import java.util.regex.Matcher;
import java.util.regex.Pattern;



/**
 * Created by BillSinla on 2016/6/24.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener{
    //账户名。密码
    private XEditText editUser, editPwd;
    //登陆， 第三方登录， 先用用看
    private TextView textRegister, textThird, textUse;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initViews() {
        editUser = (XEditText) findViewById(R.id.tv_login_user);
        editPwd = (XEditText) findViewById(R.id.tv_login_pwd);
        textRegister = (TextView) findViewById(R.id.btn_register);
        textThird = (TextView) findViewById(R.id.tv_login_third_hand);
        textUse = (TextView) findViewById(R.id.tv_fore_use);
    }

    @Override
    protected void initEvents() {
        textRegister.setOnClickListener(this);
        textThird.setOnClickListener(this);
        textUse.setOnClickListener(this);
        editPwd.setDrawableRightListener(new XEditText.DrawableRightListener() {
            @Override
            public void onDrawableRightClick(View view) {
                //有点坑，不懂为什么InputType没有129的值
                if(editPwd.getInputType() - 129 == 0){
                    editPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    editPwd.setInputType(129);
                }
            }
        });
    }

    @Override
    protected void init() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_register:
                DoRegister();
                break;
            case R.id.tv_login_third_hand:
                startLoginActivity();
                break;
            case R.id.tv_fore_use:
                exitActivity();
                break;

        }
    }

    /**
     * 注册按钮事件处理
     */
    private void DoRegister() {
        String user = editUser.getText().toString();
        String pwd = editPwd.getText().toString();
        boolean a = isEmail(user);
        boolean b = pwd.length() >= 6;

        if(isEmail(user) && pwd.length() >= 6) {
            GlobalParam.USER_KEY = "ce2aea08904c06e5e6d435b210a8c939";
            EventBus.getDefault().post(new LoginEvent());
            exitActivity();
        }else {
            Toast.makeText(RegisterActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 判断邮箱格式
     * @param strEmail
     * @return
     */
    public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strEmail);
        return m.matches();
    }

    /**
     * 返回前面的界面
     */
    private void startLoginActivity() {
        Intent i = new Intent(this, LoginActivity.class);
        startActivity(i);
        exitActivity();
    }

    private void exitActivity() {
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startLoginActivity();
    }
}
